package com.dji.sample.wayline.controller;

import com.dji.sample.wayline.model.entity.Waypoint;
import com.dji.sample.wayline.util.KmzWaypointParser;
import com.dji.sdk.common.HttpResultResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

import com.dji.sample.wayline.service.IWaypointService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * KMZ航点解析测试控制器
 * 提供上传KMZ文件并返回解析出的航点列表，便于Apifox调试。
 */
@RestController
@RequestMapping("/waypoint/api/v1")
public class WaypointController {

    @Autowired
    private IWaypointService waypointService;

    /**
     * 解析KMZ航线文件并返回航点列表。
     *
     * 请求示例（multipart/form-data）：
     * - key: file, value: kmz文件（选择文件）
     */
    @PostMapping("/waypoints/parse")
    public HttpResultResponse<List<Waypoint>> parseWaypoints(@RequestParam("file") MultipartFile file) {
        if (Objects.isNull(file) || file.isEmpty()) {
            return HttpResultResponse.error("未接收到文件。");
        }
        List<Waypoint> waypoints = KmzWaypointParser.parseWaypoint(file);
        return HttpResultResponse.success(waypoints);
    }

    @PostMapping("/waypoints/by-id")
    public HttpResultResponse<List<Waypoint>> getWaypointsByWaylineId(@RequestParam("waylineId") String waylineId) {
        if (waylineId == null || waylineId.trim().isEmpty()) {
            return HttpResultResponse.error("waylineId不能为空。");
        }
        try {
            List<Waypoint> waypoints = waypointService.getWaypointsByWaylineId(waylineId);
            return HttpResultResponse.success(waypoints);
        } catch (Exception e) {
            return HttpResultResponse.error(e.getMessage());
        }
    }

}